package ucap.com.lesanapp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import ucap.com.lesanapp.R;

/**
 * Created by wekingwang on 16/8/4.
 */
public class SettingsSelectModelView extends RelativeLayout implements View.OnClickListener {
    @Bind(R.id.rl_select_model)
    RelativeLayout rlSelectModel;
    @Bind(R.id.cb_select_state)
    CheckBox cbSelectState;
    @Bind(R.id.tv_title)
    TextView tvTitle;


    public SettingsSelectModelView(Context context) {
        super(context);
        intiView();
    }

    public SettingsSelectModelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        intiView();
    }

    public SettingsSelectModelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        intiView();
    }


    /**
     * 初始化布局
     */
    private void intiView() {
        View view = View.inflate(getContext(), R.layout.item_settings_select_model, null);
        ButterKnife.bind(this, view);
        rlSelectModel.setOnClickListener(this);
        this.addView(view);
    }

    /**
     * 设置显示的标题
     *
     * @param title
     */
    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    /**
     * 设置选中状态
     *
     * @param state
     */
    public void setSelectState(boolean state) {
        cbSelectState.setChecked(state);
    }

    @Override
    public void onClick(View v) {
        if (rlSelectModel == v) {
            cbSelectState.setChecked(!cbSelectState.isChecked());
            boolean state = cbSelectState.isChecked();
            if (onSelectStateListener != null) {
                onSelectStateListener.onSelectState(state);
            }
        }
    }

    private OnSelectStateListener onSelectStateListener;

    /**
     * 设置选中状态监听
     *
     * @param onSelectStateListener
     */
    public void setOnSelectStateListener(OnSelectStateListener onSelectStateListener) {
        this.onSelectStateListener = onSelectStateListener;
    }


    public interface OnSelectStateListener {
        void onSelectState(boolean state);
    }
}
